package com.kedou.sdk.params;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.kedou.sdk.params.bean.State;
import com.kedou.sdk.utils.WL;

/**
 * Created by Administrator on 2017/5/27 0027.
 */
public class ResponseParams<T extends Parcelable> implements Parcelable {

    public static final Creator<ResponseParams> CREATOR = new Creator<ResponseParams>() {

        @Override
        public ResponseParams createFromParcel(Parcel in) {
            return new ResponseParams(in);
        }

        @Override
        public ResponseParams[] newArray(int size) {
            return new ResponseParams[size];
        }
    };

    private static final String TAG = ResponseParams.class.getSimpleName();
//    private State state;
    private int code;
    private String msg;
    private T data;

    public ResponseParams() {
    }

    protected ResponseParams(Parcel in) {
//        state = in.readParcelable(State.class.getClassLoader());
        code = in.readInt();
        msg = in.readString();
        String className = in.readString();
        try {
            if (!TextUtils.isEmpty(className)) {
                data = in.readParcelable(Class.forName(className).getClassLoader());
            }
        } catch (ClassNotFoundException e) {
            WL.e(TAG, "ResponseParams constructor happen ClassNotFoundException.");
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeInt(code);
        dest.writeString(msg);
        if (data != null) {
            dest.writeString(data.getClass().getName());
            dest.writeParcelable(data, flags);
        }
    }

    public boolean isSuccess(){
        return code == State.SUCCESS;
    }

}
